package com.archermind.style.controller;

import com.archermind.style.bean.Instance;
import com.archermind.style.bean.Release;
import com.archermind.style.interceptor.Auth;
import com.archermind.style.service.InstanceService;
import com.archermind.style.service.OperationService;
import com.archermind.style.service.ReleaseService;
import com.archermind.style.util.Result;
import com.archermind.style.util.Search;
import com.archermind.style.util.SessionUtil;
import com.archermind.style.util.XmlUtil;
import com.feng.fengDUtil.Page;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Date;


/**
 *@Author 丰建立
 *@Date 2019/1/25 16:43
 * ["查看发布","发布样式"]
 */
@Auth("查看发布")
@RestController
@RequestMapping(value = "/release")
public class ReleaseAction {

    @Autowired
    private OperationService opService;

    @Autowired
    private ReleaseService releaseService;

    @Autowired
    private InstanceService instanceService;

    @RequestMapping(value = "/list")
    public Result listRelease(Page page, Search search) {
        releaseService.query(page,search);
        return Result.success(page);
    }

    @Auth("发布样式")
    @RequestMapping(value="/save")
    public Result save(@RequestBody Release release){
        try {
            release.setCreator(SessionUtil.getCurUserId());
            release.setCreateTime(new Date());
            releaseService.releaseStyle(release);
            releaseService.sendToRemote(release);

            opService.saveLog("添加了发布："+release.getName());
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    @Auth("发布样式")
    @RequestMapping(value="/sendToRemote")
    public Result sendToRemote(@RequestBody Release release){
        try {
            releaseService.sendToRemote(release);
            opService.saveLog("发布样式包："+release.getName());
            return Result.success(release);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    @RequestMapping(value="/zip")
    public Result zip(@RequestParam String id){
        try {
            releaseService.zip(id);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    @RequestMapping(value = "/downloadXml", method = RequestMethod.GET)
    public void downloadZip(HttpServletResponse httpServletResponse, @RequestParam("instanceId") Integer instanceId) throws IOException {

        Instance instance = instanceService.getInstance(instanceId);
        httpServletResponse.setContentType("text/xml");
  /*      httpServletResponse.setHeader("Content-disposition",
                "attachment; filename=" + new String(instance.getName().getBytes(),"ISO-8859-1") + ".xml");*/
        httpServletResponse.setHeader("Content-disposition",
                "attachment; filename="+instance.getXmlName());
        try {
            OutputStream outputStream = httpServletResponse.getOutputStream();
            Document doc = releaseService.geneXml(instance);
            //String str = doc.asXML();
            try {
                StringWriter stringWriter = XmlUtil.formatDom(doc);
                // 打印字符串,即是XML文档
                //System.out.println(stringWriter.toString());
                outputStream.write(stringWriter.toString().getBytes());
                outputStream.flush();
                outputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    @Auth("发布样式")
    @RequestMapping(value="/del")
    public Result del(String ids){
        try {
            releaseService.delReleases(ids);
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}